package com.whmnx.my.ui.viewmodel;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableArrayList;
import androidx.databinding.ObservableField;
import androidx.databinding.ObservableList;

import com.tencent.qcloud.ugckit.utils.ToastUtil;
import com.whmnx.base.bean.BeanRecordBean;
import com.whmnx.base.bean.MoneyBean;
import com.whmnx.base.constants.Constants;
import com.whmnx.base.http.CallBack;
import com.whmnx.base.http.HttpClient;
import com.whmnx.base.view.CommonDialog;
import com.whmnx.my.BR;
import com.whmnx.my.R;
import com.whmnx.my.ui.fragment.MyGoTiXianFragment;
import com.whmnx.my.ui.fragment.MyTranBalancesFragment;

import java.util.HashMap;
import java.util.Map;

import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.utils.SPUtils;
import me.tatarka.bindingcollectionadapter2.ItemBinding;

/**
 * author : huang
 * date   : 2020/9/25 0025 14:27
 * version: 1.0
 * @author Administrator
 */
public class TranBalancesModel extends ToolbarViewModel {

    public ObservableField<String> Balance = new ObservableField<>("");

    MyTranBalancesFragment mFragment;
    private double balance;

    public TranBalancesModel(@NonNull Application application) {
        super(application);
    }

    //给RecyclerView添加ObservableList
    public ObservableList<WithdrawItemViewModel3> observableList = new ObservableArrayList<>();
    //给RecyclerView添加ItemBinding
    public ItemBinding<WithdrawItemViewModel3> itemBinding = ItemBinding.of(BR.viewModel, R.layout.item_withdraw);

    //去提现
    public BindingCommand goTixianOnClickCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {

            startContainerActivity(MyGoTiXianFragment.class.getCanonicalName());
        }
    });

    //化转
    public BindingCommand huazhuanOnClickCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            String s2 = "划转至播豆余额";
            new CommonDialog(mFragment.getActivity(), R.style.dialog, s2,1, (dialog, confirm, input, s) -> {
                if (confirm) {
                    String sNum  =  input.toString();
                    if (Double.valueOf(sNum) > balance){
                        ToastUtil.toastShortMessage("当前余额不足");
                        return;
                    }
                    Map<String, Object> params = new HashMap<>();
                    params.put("uId", SPUtils.getInstance().getString(Constants.USERID));
                    params.put("bean", input);
                    //(0:交易账户;1:资产账户;)
                    params.put("type", "1");
                    HttpClient.get(mFragment.getActivity(), Constants.BEANSTRANSFER, params,new CallBack<BeanRecordBean>() {
                        @Override
                        public void onSuccess(BeanRecordBean beanRecordBean) {
                            initView();
                            dialog.dismiss();
                        }
                    });
                }
                dialog.dismiss();
            }).show();
        }
    });

    public void initToolbar(MyTranBalancesFragment myTranBalancesFragment) {
        this.mFragment = myTranBalancesFragment;
        setTitleText("交易余额");
        initView();
    }

    private void initView() {
        Map<String, Object> params = new HashMap<>();
        params.put("userInfoId", SPUtils.getInstance().getString(Constants.USERID));
        params.put("pageIndex",1);
        params.put("moneyDetail_Type",1);
        params.put("pageSize",100);
        HttpClient.post(mFragment.getActivity(), Constants.MONEYFLOWINGLIST, params,new CallBack<MoneyBean>() {
            @Override
            public void onSuccess(MoneyBean moneyBean) {
                observableList.clear();
                balance = moneyBean.getBalance();
                Balance.set(moneyBean.getBalance() + "");
                for (MoneyBean.FlowingListModelBean flowingListModelBean : moneyBean.getFlowingListModel()) {
                    observableList.add(new WithdrawItemViewModel3(TranBalancesModel.this,flowingListModelBean));
                }
            }
        });
    }
}
